//
//  HZComCollView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/26.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SVProgressHUD

class HZComCollView: UICollectionView {
    
    //懒加载可变的图片数组
    lazy var imgArray = [UIImage]()
    
    
    //添加图片的闭包
    var addImgClosure:(()->())?

   
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件
    private func setupUI(){
        
        
        backgroundColor = UIColor.whiteColor()
        
        //设置数据源
        dataSource = self
        delegate = self
        
        //注册 item
        registerClass(HZComCell.self, forCellWithReuseIdentifier: "item")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let magin = 5
        
        let itemWH = (self.frame.width-CGFloat(2*magin))/3
        
        let flow = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flow.itemSize = CGSize(width: itemWH, height: itemWH)
        
        flow.minimumInteritemSpacing = CGFloat(magin)
        flow.minimumLineSpacing = CGFloat(magin)
        
    }
    
    
    //获取图片
    func giveImg(img:UIImage){
        
        //添加到可变数组中去
        
        //判断最多只能有9张图
        if  imgArray.count < 9 {
            
            imgArray.append(img)
            
            //一更新数据就刷新
            reloadData()
            
        }else{
            
        SVProgressHUD.showErrorWithStatus("已到上限")
            return
            
        }
        
        
      
    }
    
    
    

}


extension HZComCollView:UICollectionViewDataSource,UICollectionViewDelegate {
    
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        //当没有配图时就自动隐藏(机智!!)
        self.hidden = (imgArray.count == 0)
        
        
        //如果没有或图片已满,就不添加加号按钮
        if imgArray.count == 0 || imgArray.count == 9 {
            
            return imgArray.count
        }else{
            
              return imgArray.count+1
        }
        
        
      
    }

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! HZComCell

        //如果有加号
        if indexPath.row == imgArray.count {
            
            //如果是加号按钮就隐藏取消按钮
            item.btn.hidden = true
            
            item.icon.image = UIImage(named: "compose_pic_add")
            
            return item
        }
        
        item.icon.image = imgArray[indexPath.row]
        
        item.btn.hidden = false
        
        //取消按钮的闭包回调
        item.cleanClosure = {[weak self] in
            
            self!.imgArray.removeAtIndex(indexPath.item)
            
            //更新完之后立即刷新
            self!.reloadData()
        }
        
        
        return item
    }
    
    
    //点击 cell 的时候调用
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        //如果是最后一个加号按钮
        if indexPath.row == imgArray.count {
            
//             print("晚自习")
            
            //调用添加图片的闭包回调
            //执行
            addImgClosure!()
        }
        
    }
    
    
    
    
    
    
    
}


